﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Studio77.RuneMaster.Entities;
using Studio77.RuneMaster.Core;
using System.Windows.Media.Imaging;

namespace Studio77.RuneMaster.Views
{
    public partial class TopToolbar : UserControl
    {
        public TopToolbar()
        {
            InitializeComponent();
            toolImages.Clear();
            toolImages.Add(imgTool1);
            toolImages.Add(imgTool2);
            toolImages.Add(imgTool3);
        }

        int currentScore = 0;
        int spriteCount = 0;
        StageInfo currentStageInfo = null;
        List<Image> toolImages = new List<Image>();

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="stageInfo"></param>
        internal void InitToolBar(StageInfo stageInfo)
        {
            currentScore = 0;
            currentStageInfo = stageInfo;
            spriteCount = stageInfo.SpriteCount;
            this.txtStageName.Text = stageInfo.StageName;
            this.txtSpriteCount.Text = stageInfo.SpriteCount.ToString();
            this.txtScoreStatus.Text = currentScore + "/" + stageInfo.AimScore;
            this.gameProcess.Maximum = stageInfo.AimScore;
            this.gameProcess.Minimum = 0;
            this.gameProcess.Value = 0;

            //更新界面显示
            if (StageManager.Instance.CurrentGameMode == "e")
            {
                this.imgGameModeEasy.Visibility = System.Windows.Visibility.Visible;
                this.imgGameModeHard.Visibility = System.Windows.Visibility.Collapsed;
                this.imgGameModeNormal.Visibility = System.Windows.Visibility.Collapsed;
            }
            else if (StageManager.Instance.CurrentGameMode == "n")
            {
                this.imgGameModeEasy.Visibility = System.Windows.Visibility.Collapsed;
                this.imgGameModeHard.Visibility = System.Windows.Visibility.Collapsed;
                this.imgGameModeNormal.Visibility = System.Windows.Visibility.Visible;
            }
            else if (StageManager.Instance.CurrentGameMode == "h")
            {
                this.imgGameModeEasy.Visibility = System.Windows.Visibility.Collapsed;
                this.imgGameModeHard.Visibility = System.Windows.Visibility.Visible;
                this.imgGameModeNormal.Visibility = System.Windows.Visibility.Collapsed;
            }

            UpdateUsedToolUI();
        }

        internal void AddScore(int score)
        {
            currentScore += score;
            this.txtScoreStatus.Text = currentScore + "/" + currentStageInfo.AimScore;
            this.gameProcess.Value = currentScore > currentStageInfo.AimScore ? currentStageInfo.AimScore : currentScore;
        }

        internal void AddSprite(int count)
        {
            spriteCount += count;
            this.txtSpriteCount.Text = spriteCount.ToString();
        }

        internal void SubSprite(int count)
        {
            spriteCount -= count;
            this.txtSpriteCount.Text = spriteCount.ToString();
        }

        /// <summary>
        /// 更新道具使用信息
        /// </summary>
        internal void UpdateUsedToolUI()
        {
            for (int i = 0; i < StageManager.Instance.CurrentUsedTool.Count; i++)
            {
                ToolType type = StageManager.Instance.CurrentUsedTool[i];
                if (type != ToolType.NoTool)
                {
                    string uri = string.Empty;
                    switch (type)
                    {
                        case ToolType.Add500:
                            uri = "/Images/add500.png";
                            break;
                        case ToolType.CollectSprite:
                            uri = "/Images/CollectSprite.png";
                            break;
                        case ToolType.CreateNewSpriteRune:
                            uri = "/Images/AddSpriteRune.png";
                            break;
                        case ToolType.ReArrange:
                            uri = "/Images/ReArrange.png";
                            break;
                        case ToolType.ScoreMutil2:
                            uri = "/Images/score2.png";
                            break;
                        case ToolType.SpriteMutil2:
                            uri = "/Images/Sprite2.png";
                            break;
                    }

                    BitmapImage bitmap = new BitmapImage(new Uri(uri, UriKind.RelativeOrAbsolute));
                    toolImages[i].Source = bitmap;
                    toolImages[i].Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    toolImages[i].Visibility = System.Windows.Visibility.Collapsed;
                }
            }
        }

        internal void SetLeftSprite(int count)
        {
            spriteCount = count;
            this.txtSpriteCount.Text = spriteCount.ToString();
        }
    }
}
